package com.chuzhong.sf.gui;

import com.chuzhong.sf.api.admin_api;
import com.chuzhong.sf.api.login_api;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author 初衷.
 * @Date 2020/10/15
 **/
public class Admin_User extends JFrame {
    private DefaultTableModel model = null;
    private static JLabel j_name;//左上角用户名
    private static JLabel j_quota;//上边中间额度
    private static JTable table;// 声明表格
    private static JButton j_cx;
    private static JButton j_cxall;
    private static JPanel jl_1;//用户主页的版面
    private static JLabel jl_liushui;//合计
    private static JLabel jl_shouyi;//合计
    private static JTextField j_time;
    private long d_liushui;
    private long d_shouyi;
    private Object [][]data=null;
    private Object [][]datas=null;
    private Object [][]data1=null;
    public Admin_User(String username, String edu, Integer id) throws IOException {

        Font font = new Font("宋体", Font.PLAIN, 20);//设置字体
        jl_1 = new JPanel();
        super.setTitle("单个用户页面");

        j_name = new JLabel("用户名:"+username);
//        j_name.setBounds(20, 10, 30, 50);
        j_name.setFont(font);

        j_quota = new JLabel("额度:"+edu);
//        j_quota.setBounds(200, 10, 30, 50);
        j_quota.setFont(font);


        j_cx = new JButton("查询");
        j_cx.setFont(font);
        j_cxall = new JButton("全部");
        j_cxall.setFont(font);

        j_time = new JTextField("请选择时间");
        j_time.setFont(font);

        // 定义日历控件面板类
        CalendarPanel p = new CalendarPanel(j_time, "yyyy-MM-dd");
        p.initCalendarPanel();

        JLabel l = new JLabel("日历面板");

        String[] titles = {"日期", "流水", "收益"};
        List<Map> maps = login_api.queryuserInfo_m(id);//获取当前用户的数据
        data=new Object[maps.size()][titles.length]; //将数据遍历为所需的格式
        for (int i=0;i<maps.size();i++){
            for (int j=0;j<titles.length;j++){
                data[i][0]  = maps.get(i).get("info_time");
                data[i][1]  = null;
                if(maps.get(i).get("info_liushui").equals(0)){
                    data[i][1] = 0;
                }else{
                    data[i][1]  = maps.get(i).get("info_liushui");
                }
                data[i][2]  = null;
                if( maps.get(i).get("info_shouyi").equals(0)){
                    data[i][2] = 0;
                }else{
                    data[i][2]  =  maps.get(i).get("info_shouyi");

                }
            }

            d_shouyi = d_shouyi +Long.parseLong((String) data[i][2].toString());
            d_liushui =d_liushui+ Long.parseLong((String) data[i][1].toString());
            jl_liushui  = new JLabel("总流水："+d_liushui);
            jl_shouyi  = new JLabel("总收益："+d_shouyi);
        }
        jl_liushui.setFont(font);
        jl_shouyi.setFont(font);
        JPanel panel = new JPanel();
        panel.add(jl_liushui);
        panel.add(jl_shouyi);

        jl_1.add(j_name);
        jl_1.add(j_quota);
        jl_1.add(j_time);
        jl_1.add(j_cx);
        jl_1.add(j_cxall);
        p.add(l);
        add(p);
        add(jl_1,BorderLayout.NORTH);
        DefaultTableModel defaultTableModel = new DefaultTableModel(data, titles);
        table = new JTable(defaultTableModel);
        JTableHeader tableHeader = this.table.getTableHeader();
        tableHeader.setFont(font);
        table.setFont(font);
        table.setRowHeight(25);

        add(new JScrollPane(table),BorderLayout.CENTER);
        add(panel, BorderLayout.SOUTH);
        setSize(1000, 700);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
//        setLocation(400,200);
        ActionListener all_query = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Map> maps1 = null;//获取当前用户的数据
                try {
                    maps1 = login_api.queryuserInfo(id);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                datas=new Object[maps1.size()][titles.length]; //将数据遍历为所需的格式
                d_shouyi = 0;
                d_liushui = 0;
                for (int i=0;i<maps1.size();i++){
                    for (int j=0;j<titles.length;j++){
                        datas[i][0]  = maps1.get(i).get("info_time");
                        datas[i][1]  = maps1.get(i).get("info_liushui");
                        datas[i][2]  = maps1.get(i).get("info_shouyi");
                    }
                    d_shouyi = d_shouyi +Long.parseLong((String) datas[i][2].toString());
                    d_liushui =d_liushui+ Long.parseLong((String) datas[i][1].toString());
                }
                jl_liushui  = new JLabel("总流水："+d_liushui);
                jl_shouyi  = new JLabel("总收益："+d_shouyi);
                jl_liushui.setFont(font);
                jl_shouyi.setFont(font);
                DefaultTableModel tabModel = new DefaultTableModel(datas,titles);
                panel.removeAll();
                panel.add(jl_liushui);
                panel.add(jl_shouyi);
                panel.repaint();
                add(panel, BorderLayout.SOUTH);
                table.setModel(tabModel);
                j_cxall.setEnabled(false);
                table.setEnabled(true);
                setVisible(true);
            }
        };
        j_cxall.addActionListener(all_query);
        ActionListener get_time = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String text = j_time.getText();
                List<Map> maps1 =null;
                try {
                    maps1 = login_api.queryuserInfo_get_time(id, text);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                data1=new Object[maps1.size()][titles.length]; //将数据遍历为所需的格式
                d_shouyi = 0;
                d_liushui = 0;
                for (int i=0;i<maps1.size();i++){
                    for (int j=0;j<titles.length;j++){
                        data1[i][0]  = maps1.get(i).get("info_time");
                        data1[i][1]  = null;
                        if(maps1.get(i).get("info_liushui").equals(0)){
                            data1[i][1] = "0";
                        }else{
                            data1[i][1]  = maps1.get(i).get("info_liushui");
                        }
                        data1[i][2]  = null;
                        if( maps1.get(i).get("info_shouyi").equals(0)){
                            data1[i][2] = "0";
                        }else{
                            data1[i][2]  =  maps1.get(i).get("info_shouyi");
                        }
                    }
                    d_shouyi = d_shouyi +Long.parseLong(data1[i][2].toString());
                    d_liushui =d_liushui+ Long.parseLong(data1[i][1].toString());
                }
                jl_liushui  = new JLabel("总流水："+d_liushui);
                jl_shouyi  = new JLabel("总收益："+d_shouyi);
                jl_liushui.setFont(font);
                jl_shouyi.setFont(font);
                panel.removeAll();

                panel.add(jl_liushui);
                panel.add(jl_shouyi);
                panel.repaint();
                add(panel, BorderLayout.SOUTH);
                DefaultTableModel tabModel1 = new DefaultTableModel(data1,titles);
                table.setModel(tabModel1);
                table.setEnabled(true);
                j_cxall.setEnabled(true);
                setSize(1000,710);
                setVisible(true);
            }
        };
        j_cx.addActionListener(get_time);
        TableModelListener tableModelListener = new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                int column = e.getColumn();
                int row = e.getFirstRow();
                System.out.println(column);
                System.out.println(row);
                Object valueAt = table.getValueAt(row, column);
                Object info_time = maps.get(row).get("info_time");
                //1   流水  2  收益
                System.out.println(column+""+valueAt+"+"+id+info_time);
                int i = Integer.parseInt(valueAt.toString());
                Integer info_liushui = null;
                Integer info_shouyi = null;
                if(column==1){
                    info_liushui = i;
                }else if(column ==2){
                    info_shouyi = i;
                }
                try {
                    admin_api.admin_info_f_t(id, (String) info_time,info_liushui,info_shouyi,column);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(null,"编辑数据完成");
            }
        };
        defaultTableModel.addTableModelListener(tableModelListener);
    }

    public static void main(String[] args) {
    }
}
